<template>
  <div
    class="modal"
    :id="id"
    tabindex="-1"
    role="dialog"
    aria-labelledby="myModalLabel"
    aria-hidden="true"
  >
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="myModalLabel">
            <slot>上传Excel文件并导入数据</slot>
          </h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form id="uploadFileForm" method="post" enctype="multipart/form-data">
            <div class="row mb-2">
              <label>选择需要上传的文件</label>
              <input
                type="file"
                class=""
                name="upfile"
                id="inputFileName"
              />
              <span class="text-muted small">
                文件名请不要有中文或全角字符，请使用字母或数字命名文件名。
                <br />
                <span class="text-danger">{{ errmsg }}</span>
              </span>
            </div>

            <div class="row mb-2" v-if="!hideCustomer">
              <label class="control-label col-3 text-end">企业名称</label>
              <div class="col-9" v-if="canShowSelect2 == 1">
                <my-select-customer
                  :supplierId.sync="customerId"
                  name="customerId"
                  :id="id + `_customer`"
                  :dropdownParentId="id"
                ></my-select-customer>
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button
            type="button"
            class="btn btn-primary btn-lg ms-2"
            @click.stop="ok()"
          >
            开始上传
          </button>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import $ from "jquery";
import MySelectCustomer from "@/components/my-select2-customer.vue";

export default {
  props: {
    id: {
      type: String,
      default: "uploadPassengerExcelModal",
    },
    nullable: {
      type: Boolean,
      default: true,
    },
    hideCustomer: {
      type: Boolean,
      default: true
    }
  },
  components: {
    MySelectCustomer,
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      errmsg: "",
      customerId: 0,

      myModal: null,
      canShowSelect2: 0
    };
  },
  methods: {
    ok: function () {
      const filename = $("#inputFileName").val();

      if (filename.length === 0) {
        this.errmsg = "请选择需要上传的文件";
        return;
      }

      this.hideModal();

      const formData = new window.FormData($("#uploadFileForm")[0]);

      console.log(formData);

      this.resolve({
        data: formData,
      });
    },
    modal: function () {
      this.errmsg = "";
      $("#inputFileName").val("");

      const self = this;
      const myModalEl = $(this.$el).get(0);
      myModalEl.addEventListener("shown.bs.modal", function () {
        console.log("shown.bs.modal");
        self.canShowSelect2 = 1;
      });

      this.showModal();

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
